//
//  SingleBarView.m
//  DebugDemo
//
//  Created by XFB on 2019/5/17.
//  Copyright © 2019 ZhouCheng. All rights reserved.
//

#import "SingleBarView.h"

@interface SingleBarView ()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) UIBezierPath *bezierPath;

@end


@implementation SingleBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _shapeLayer = [CAShapeLayer layer];
        _shapeLayer.strokeColor = RandomColor.CGColor;
        _shapeLayer.lineCap = kCALineCapButt;
        _shapeLayer.frame = self.bounds;
        [self.layer addSublayer:_shapeLayer];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = 2;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnimation.fromValue = @0.0f;
    pathAnimation.toValue = @1.0f;
    [_shapeLayer addAnimation:pathAnimation forKey:nil];
}

- (void)setBarWidth:(CGFloat)barWidth
{
    _barWidth = barWidth;
    _shapeLayer.lineWidth = barWidth;
    [self setSingleBarProgress];
}

- (void)setSingleBarProgress
{
    _bezierPath = [UIBezierPath bezierPath];
    NSLog(@"1--------%f", self.bounds.origin.x+self.bounds.size.width/2);
    NSLog(@"2--------%f", self.bounds.size.height);
    NSLog(@"3--------%f  \n\n", self.bounds.size.height*(1-_barProgress));

    // {{0, 0}, {20, 226}}
    [_bezierPath moveToPoint:CGPointMake(self.bounds.origin.x+self.bounds.size.width/2, self.bounds.size.height)];
    [_bezierPath addLineToPoint:CGPointMake(self.bounds.origin.x+self.bounds.size.width/2, self.bounds.size.height*(1-_barProgress))];
    [_bezierPath setLineWidth:_barWidth];
    [_bezierPath setLineCapStyle:kCGLineCapSquare];
    _shapeLayer.strokeEnd = 1.0;
    _shapeLayer.path = _bezierPath.CGPath;
}

- (void)setBarProgress:(float)barProgress
{
    _barProgress = barProgress;
    [self setSingleBarProgress];
}

@end
